﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JYStockAnalyser.DataGeter
{
    public partial class FrmFiveAddingORSubtractingGeter : Form
    {
        WebBrowser webb1;
        HtmlDocument htmldoc1;
        System.Threading.Thread th;

        public FrmFiveAddingORSubtractingGeter()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
            Control.CheckForIllegalCrossThreadCalls = false;
            webb1 = new WebBrowser();
            webb1.Navigate("about:blank");
            htmldoc1 = webb1.Document.OpenNew(true);
        }

        private void FrmFiveAddingORSubtractingGeter_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string type = (string)this.comboBox1.SelectedItem;
            if (type.Equals("主力五日增仓"))
            {
                m_getFiveAddingData();
                MessageBox.Show("完成!");
            }
            else if (type.Equals("主力五日减仓"))
            {
                m_getFiveSubtactingData();
                MessageBox.Show("完成!");
            }
        }

        /// <summary>
        /// 获取五日主力增仓数据
        /// </summary>
        private void m_getFiveAddingData()
        {
            string strWebContent = Tool.GetContentFromUrll("http://stock1.finance.qq.com/c/slzlzc5d.htm");

            int start1 = strWebContent.IndexOf("<TABLE cellSpacing=0 cellPadding=0 width=910 border=0>");

            int end1 = strWebContent.IndexOf("</TABLE>", start1);
            string content1 = strWebContent.Substring(start1, end1 - start1 + 8);

            htmldoc1.OpenNew(true);
            htmldoc1.Write(content1);

            string strDT = htmldoc1.GetElementsByTagName("TR")[0].GetElementsByTagName("TD")[1].InnerText.Substring(0, 10).Replace(" ", "").Replace("（", "").Replace("每", "");
            DateTime dt = DateTime.Parse(strDT);
          
            int start2 = strWebContent.IndexOf("<TABLE cellSpacing=1 cellPadding=1 width=910 bgColor=#cccccc border=0>", end1);
            int end2 = strWebContent.IndexOf("</TABLE>", start2);
            string content2 = strWebContent.Substring(start2, end2 - start2 + 8);

            htmldoc1.OpenNew(true);
            htmldoc1.Write(content2);
            HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

            for (int i = 1; i < trs.Count; i++)
            {
                HtmlElementCollection tds = trs[i].GetElementsByTagName("TD");

                int order = int.Parse(tds[0].InnerText.Trim());
                string stockNO = tds[1].InnerText.Trim();
                string stockName = tds[2].InnerText.Trim();
                decimal currentPrice = -77777.77m;
                try { currentPrice = decimal.Parse(tds[3].InnerText.Trim()); }
                catch {};

                decimal upORDownRange = -77777.77m;
                try { upORDownRange = decimal.Parse(tds[4].InnerText.Trim()); }
                catch { };

                int tradingVolumn = -77777;
                try { tradingVolumn = int.Parse(tds[5].InnerText.Trim()); }
                catch { };

                decimal money = -77777.77m;
                try { money = decimal.Parse(tds[6].InnerText.Trim()); }
                catch { };

                decimal addORSubtractMoney = -77777.77m;
                try { addORSubtractMoney = decimal.Parse(tds[7].InnerText.Trim()); }
                catch { };


                DataTool.InsertFiveAddingItem(order, stockNO, stockName, currentPrice, upORDownRange, tradingVolumn, money, addORSubtractMoney, dt);
            }
        }
        /// <summary>
        /// 获取五日主力减仓数据
        /// </summary>
        private void m_getFiveSubtactingData()
        {
            string strWebContent = Tool.GetContentFromUrll("http://stock1.finance.qq.com/c/slzljc5d.htm");

            int start1 = strWebContent.IndexOf("<TABLE cellSpacing=0 cellPadding=0 width=910 border=0>");

            int end1 = strWebContent.IndexOf("</TABLE>", start1);
            string content1 = strWebContent.Substring(start1, end1 - start1 + 8);

            htmldoc1.OpenNew(true);
            htmldoc1.Write(content1);

            string strDT = htmldoc1.GetElementsByTagName("TR")[0].GetElementsByTagName("TD")[1].InnerText.Substring(0, 10).Replace(" ", "").Replace("（", "").Replace("每", "");
            DateTime dt = DateTime.Parse(strDT);

            int start2 = strWebContent.IndexOf("<TABLE cellSpacing=1 cellPadding=1 width=910 bgColor=#cccccc border=0>", end1);
            int end2 = strWebContent.IndexOf("</TABLE>", start2);
            string content2 = strWebContent.Substring(start2, end2 - start2 + 8);

            htmldoc1.OpenNew(true);
            htmldoc1.Write(content2);
            HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

            for (int i = 1; i < trs.Count; i++)
            {
                HtmlElementCollection tds = trs[i].GetElementsByTagName("TD");

                int order = int.Parse(tds[0].InnerText.Trim());
                string stockNO = tds[1].InnerText.Trim();
                string stockName = tds[2].InnerText.Trim();
                decimal currentPrice = -77777.77m;
                try { currentPrice = decimal.Parse(tds[3].InnerText.Trim()); }
                catch { };

                decimal upORDownRange = -77777.77m;
                try { upORDownRange = decimal.Parse(tds[4].InnerText.Trim()); }
                catch { };

                int tradingVolumn = -77777;
                try { tradingVolumn = int.Parse(tds[5].InnerText.Trim()); }
                catch { };

                decimal money = -77777.77m;
                try { money = decimal.Parse(tds[6].InnerText.Trim()); }
                catch { };

                decimal addORSubtractMoney = -77777.77m;
                try { addORSubtractMoney = decimal.Parse(tds[7].InnerText.Trim()); }
                catch { };


                DataTool.InsertFiveSubtractingItem(order, stockNO, stockName, currentPrice, upORDownRange, tradingVolumn, money, addORSubtractMoney, dt);
            }
        }
    }
}
